﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqAndLambda.Modules
{
    public class Lef和多From子句
    {
        public static void mian()
        {
            #region Lef 关键字用法
            var Products = from pro in GetProducts()
                           where pro.Price * pro.Storage > 20000
                           select new { pro.ProductName, Price = pro.Price * pro.Storage };
            Products = from pro in GetProducts()
                       let price = pro.Price * pro.Storage
                       where price > 20000
                       select new { pro.ProductName, Price = price };
            #endregion

            #region 多from子句
            var Products1 = from p in GetProducts()
                            from c in GetCategories()
                            where p.CategoryId == c.CategoryId
                            select new { products = p, categories = c };
            #endregion

            foreach (var item in Products)
            {
                Console.WriteLine($"名称:{item.ProductName},总价{item.Price}");
            }

            Console.WriteLine("-------------------------------------------------------");
            foreach (var item in Products1)
            {
                Console.WriteLine($"名称:{item.products.ProductName},分类名称:{item.categories.CategoryName}");
            }
        }


        public static List<Category> GetCategories()
        {
            List<Category> list = new List<Category>(){
            new Category(){ CategoryId=1, CategoryName="服装" },
            new Category(){ CategoryId=2, CategoryName="食品" },
            new Category(){ CategoryId=3, CategoryName="办公" }
        };
            return list;
        }

        public static List<Product> GetProducts()
        {
            List<Product> list = new List<Product>(){
            new Product(){ ProductId=1,  ProductName="雪中飞羽绒服" , Price=998, Storage=588, CategoryId=1},
            new Product(){ ProductId=2, ProductName="安踏鞋子" , Price=198, Storage=288, CategoryId=1},
            new Product(){ ProductId=3, ProductName="短袖", Price=95, Storage=88, CategoryId=1 },
            new Product(){ ProductId=4, ProductName="旺旺雪饼" ,Price=16.5, Storage=18, CategoryId=2  },
            new Product(){ ProductId=5, ProductName="汇源果汁" ,Price=6, Storage=12, CategoryId=2 },
            new Product(){ ProductId=6, ProductName="英雄钢笔"  ,Price=12.5, Storage=59, CategoryId=3 }
        };
            return list;
        }
    }


}
